package com.example.uchain.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.uchain.demo.Result;
import com.example.uchain.demo.WorksInformation;
import com.example.uchain.service.WorksTochainService;
import com.example.uchain.utils.WorkCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorksTochainServicelmpl implements WorksTochainService {

//    @Autowired
//    WeBASEUtils weBASEUtils;

    //信息上链
    @Override
    public Result AddChain(WorksInformation worksInformation) {

        if (worksInformation.getWid() == null ||
                StrUtil.isEmpty(worksInformation.getWorksName()) ||
                StrUtil.isEmpty(worksInformation.getWorksProperty()) ||
                StrUtil.isEmpty(worksInformation.getIntroduce()) ||
                StrUtil.isEmpty(worksInformation.getCreateAddress()) ||
                StrUtil.isEmpty(worksInformation.getPublishAddress())
        ) {
            return Result.error(String.valueOf(Result.PARAM_EMPTY));
        } else {
            System.out.println(worksInformation);
        }
        WorkCode workCode = new WorkCode();
        String _result = String.valueOf(workCode.addWork(worksInformation.getWid(), worksInformation.getWorksName(), worksInformation.getWorksProperty(), worksInformation.getIntroduce(), worksInformation.getCreateAddress(), worksInformation.getPublishAddress()));
        log.info("_result:{}", _result);
        if ((_result.equals("null"))) {
            return Result.error(String.valueOf(Result.CONTRACT_ERROR));
        }
        else {
            JSONObject _resultJson = JSONUtil.parseObj(_result);
            if (!_resultJson.containsKey("statusOK") || !_resultJson.getBool("statusOK")) {
                return Result.error(String.valueOf(Result.CONTRACT_ERROR));
            } else {
                // 创建一个 Map 对象，将数据放入其中
                Map<String, Object> data = new HashMap<>();

                String transactionHash = _resultJson.getStr("transactionHash");
                String transactionIndex = _resultJson.getStr("gasUsed");
                String blockNumber = _resultJson.getStr("blockNumber");
                String blockHash = _resultJson.getStr("hash");
                String from = _resultJson.getStr("from");
                String to = _resultJson.getStr("to");
                String message = _resultJson.getStr("message");
                String statusOK = _resultJson.getStr("statusOK");

                data.put("transactionHash", transactionHash);
                data.put("transactionIndex", transactionIndex);
                data.put("blockNumber", blockNumber);
                data.put("blockHash", blockHash);
                data.put("from", from);
                data.put("to", to);
                data.put("message", message);
                data.put("statusOK", statusOK);

                // 将 Map 对象转换为 JSON 字符串
                String jsonData = JSONUtil.toJsonStr(data);

                // 将 JSON 字符串放入一个对象中并返回给前端
                Map<String, Object> result = new HashMap<>();
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", jsonData);

                return Result.success(result);
            }
        }
    }
}
